package org.example.tomcat;


import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;


//覆写默认实现 即为自己的简洁实现  传入socket  解析请求行  请求头  请求体
public class MyRequest extends AbstractHttpServletRequest {
    private Socket socket;
    private String method="GET";
    private String url="/";
    private String protocol="HTTP/1.1";
    private Map<String, String> headers;
    private String body;

    public MyRequest(Socket socket) {
        this.socket = socket;
        this.headers = new HashMap<>();
        this.body = "";
        parseRequest();

    }



    public void parseRequest() {
        try {
            InputStream inputStream = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int bytesRead;

            StringBuilder request = new StringBuilder();
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                request.append(new String(buffer, 0, bytesRead));
                if (request.toString().contains("\r\n\r\n")) {
                    break;
                }
            }
            //打印request
            System.out.println(request.toString());

            // 解析请求内容
            String requestContent = request.toString();
            String[] lines = requestContent.split("\r\n");

            if (lines.length > 0) {
                String[] requestLine = lines[0].split(" ");
                if (requestLine.length == 3) {
                    method = requestLine[0]; // 请求方法，如 GET, POST
                    url = requestLine[1];    // 请求 URL
                    protocol = requestLine[2]; // HTTP 协议版本，如 HTTP/1.1
                }
            }

            // 解析请求头
            for (int i = 1; i < lines.length; i++) {
                String[] headerParts = lines[i].split(": ", 2);
                if (headerParts.length == 2) {
                    headers.put(headerParts[0], headerParts[1]);
                }
            }

            // 如果是 POST 请求，读取请求体
            if ("POST".equalsIgnoreCase(method)) {
                int contentLength = Integer.parseInt(headers.getOrDefault("Content-Length", "0"));
                if (contentLength > 0) {
                    byte[] bodyBytes = new byte[contentLength];
                    inputStream.read(bodyBytes);
                    body = new String(bodyBytes);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String getRequestURI() {
        return url;
    }

    @Override
    public StringBuffer getRequestURL() {
        return new StringBuffer(url);
    }

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public String getProtocol() {
        return protocol;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public String getBody() {
        return body;
    }

    public Socket getSocket() {
        return socket;
    }
}
